package com.example.demo2.controller;

import com.example.demo2.bean.HomeBean;
import com.example.demo2.bean.UserBean;
import com.example.demo2.bean.YourBean;
import com.example.demo2.service.HomeBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})
public class HomeBeanController {

    @Autowired
    HomeBeanService homeBeanService;
    // 查询所有
    @RequestMapping("/home/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<HomeBean> list = homeBeanService.list();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    //删除
    @RequestMapping("/home/delete")
    public Map<String, Object> delete(@RequestBody HomeBean homeBean) {
        Map<String, Object> map = new HashMap<>();
        try {
            homeBeanService.deleteById(homeBean.getHome_id());
            map.put("success", true);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    // 修改
    @RequestMapping("/home/update")
    public Map<String, Object> update(@RequestBody HomeBean homeBean) {
        Map<String, Object> map = new HashMap<>();
        HomeBean homeBean1 = homeBeanService.update(homeBean);
        if (homeBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", homeBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 添加
    @RequestMapping("/home/addone")
    public Map<String, Object> addone(@RequestBody HomeBean homeBean) {
        Map<String, Object> map = new HashMap<>();
        HomeBean homeBean1 = homeBeanService.addone(homeBean);
        if (homeBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", homeBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
}
